import axios from 'axios'
import utils from '@/utils/encode'
// 优先获取
function  getRequestConfigure(){
	let requestConfigure = {
		isEncryption: true, 
		model: "development",
		// model: "custom",
		// model : 'production',  
		requestUrl: {
			productionUrl: 'https://ctrl.yunshucs.cn',
			developmentUrl: 'https://wepay.yunshucs.cn', 
			customUrl: 'http://192.168.1.23:48080'
		},
	}; 
	Object.assign(requestConfigure,uni.getStorageSync('requestConfigure')||{});
	return requestConfigure
}
let service = axios.create({
	timeout: 5000,
	sslVerify: false
});
service.getRequestConfigure =  getRequestConfigure; 
 
var url = '';
let platform = uni.getSystemInfoSync().platform;
service.interceptors.request.use(
	config => { 
		let  requestConfigure = getRequestConfigure(); 
		config.baseURL = requestConfigure.requestUrl[`${requestConfigure.model}Url`];
		// token 信息
		const token = uni.getStorageSync('Authorization');
		let RelId = uni.getStorageSync('Rel-Id');
		if(RelId){
			config.headers['Rel-Id'] = RelId;
		}
		if (token) {
			config.headers['Authorization'] = `Bearer ${token}`;
		}
		config.headers['System-Mark'] = "WMS_MP";
		// #ifdef MP-WEIXIN 
		// 微信
		config.headers['loginAppType'] = "MiniAPP"; 
		// #endif 
		config.headers['Tenant-Id'] = requestConfigure.model === 'production' ? 0 : 1;
		//判断全局加密和区域加密 区域加密默认开启
		if (requestConfigure.isEncryption&&(config.isEncryption??true)) { 
			const data = { // 用于存储加密
				params: '' // 加密后的密文
			}
			// 要加密
			data.params = utils.encrypt(JSON.stringify(config.data))
			config.data = data
		} else {
			console.log('参数', config.url, '参数加密前', config.data)
			url = config.url
		}
		return config
	},
	error => {
		return Promise.reject(error)
	}
)

service.interceptors.response.use(
	response => {
		let  requestConfigure = getRequestConfigure(); 
		const res = response.data;
		if (requestConfigure.isEncryption) {
			// 生产环境，进行加密解密，不输出日志
			if (res?.encrypt == 1) {
				// 加密的数据，需要解密
				const dataParam = JSON.parse(utils.decrypt(res.data));
				res.data = JSON.stringify(dataParam) === '{}' ? null : dataParam;
				// console.log('返回', response.config.url, '返回结果', res)
			}
		} else {
			// console.log('返回', response.config.url, '返回结果未加密', res)
		}


		if (res.code !== 0) {
			if (res.code === 401 || res.code === 433) { 
				uni.clearStorageSync();
			}
			if (!response.config.isNotMsg) {
				uni.showToast({
					title: res.msg,
					icon: 'none',
					duration: 2000
				});
			}
		}

		return res
	},
	error => {
		return Promise.reject(error)
	}
)

// #ifndef H5
service.defaults.adapter = function(config) {
	return new Promise((resolve, reject) => {
		var settle = require('axios/lib/core/settle');
		var buildURL = require('axios/lib/helpers/buildURL');
		uni.request({
			method: config.method.toUpperCase(),
			url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
			header: config.headers,
			data: config.data,
			dataType: config.dataType,
			responseType: config.responseType,
			sslVerify: config.sslVerify,
			complete: function complete(response) {
				response = {
					data: response.data,
					status: response.statusCode,
					errMsg: response.errMsg,
					header: response.header,
					config: config
				};
				settle(resolve, reject, response);
			}
		})
	})
}
// #endif
export default service